#include "stdafx.h"

#include <altcecrt.h>

#include "ComHelper.h"

#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>

#ifndef SYSTEM_API_OUTPUT_DEBUG_INFO
#undef dbgprintf
#define dbgprintf //
#endif

extern void debugprintf(const char *str, ...);

CComHelper::CComHelper(void)
: pImgFactory(NULL)
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	CoCreateInstance (
		CLSID_ImagingFactory,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_IImagingFactory,
		(void **)&pImgFactory);
}

CComHelper::~CComHelper(void)
{
	if (pImgFactory)
	{
		((IImagingFactory *)pImgFactory)->Release();
		pImgFactory = NULL;
	}

	CoUninitialize();
}

HBITMAP CComHelper::CreaetBitmapFromAnyBuffer(const VOID * pimgdata, UINT size, unsigned short * pWidth, unsigned short * pHeight, int nBpps)
{
	HBITMAP hBitmap = NULL;
	IImage *pImage = NULL;

	if (pImgFactory)
	{
		// Load the image from the file data.
		if (SUCCEEDED(((IImagingFactory *)pImgFactory)->CreateImageFromBuffer(
			pimgdata,
			size,
			BufferDisposalFlagNone,
			&pImage
			)))
		{
			// Get width and height of the image
			ImageInfo image;
			if (SUCCEEDED(pImage->GetImageInfo(&image)))
			{
				HDC dcMemory = CreateCompatibleDC(NULL);
				hBitmap = CreateBitmap(image.Width, image.Height, 1, nBpps, NULL);

				if (pWidth)
				{
					*pWidth = image.Width;
				}
				if (pHeight)
				{
					*pHeight = image.Height;
				}

				if (dcMemory && hBitmap)
				{
					HBITMAP hOldBitmap = (HBITMAP)SelectObject(dcMemory, hBitmap);

					// Draw the image.
					RECT rc = {0, 0, image.Width, image.Height};
					pImage->Draw(dcMemory, &rc, NULL);
					pImage->Release();

					SelectObject(dcMemory, hOldBitmap);
				}

				if (dcMemory)
				{
					DeleteDC(dcMemory);
					dcMemory = NULL;
				}
			}
		}
	}

	return hBitmap;
}
HBITMAP CComHelper::CreaetBitmapFromFile(WCHAR * path, unsigned short * pWidth, unsigned short * pHeight, int nBpps)
{
	HBITMAP hBitmap = NULL;
	IImage *pImage = NULL;

	if (pImgFactory)
	{
		// Load the image from the file data.
		if (SUCCEEDED(((IImagingFactory *)pImgFactory)->CreateImageFromFile(
			path, 
			&pImage
			)))
		{
			// Get width and height of the image
			ImageInfo image;
			if (SUCCEEDED(pImage->GetImageInfo(&image)))
			{
				HDC dcMemory = CreateCompatibleDC(NULL);
				hBitmap = CreateBitmap(image.Width, image.Height, 1, nBpps, NULL);

				if (pWidth)
				{
					*pWidth = image.Width;
				}
				if (pHeight)
				{
					*pHeight = image.Height;
				}

				if (dcMemory && hBitmap)
				{
					HBITMAP hOldBitmap = (HBITMAP)SelectObject(dcMemory, hBitmap);

					// Draw the image.
					RECT rc = {0, 0, image.Width, image.Height};
					pImage->Draw(dcMemory, &rc, NULL);
					pImage->Release();

					SelectObject(dcMemory, hOldBitmap);
				}

				if (dcMemory)
				{
					DeleteDC(dcMemory);
					dcMemory = NULL;
				}
			}
		}
	}

	return hBitmap;
}
void CComHelper::DrawPngToHDC(WCHAR * path, HDC dc)
{
	IImage *pImage = NULL;

	if (pImgFactory)
	{
		debugprintf("CComHelper 1");

		// Load the image from the file data.
		if (SUCCEEDED(((IImagingFactory *)pImgFactory)->CreateImageFromFile(
			path, 
			&pImage
			)))
		{
			debugprintf("CComHelper 2");

			// Get width and height of the image
			ImageInfo image;
			if (SUCCEEDED(pImage->GetImageInfo(&image)))
			{
				if (dc)
				{
					debugprintf("CComHelper 3");

					// Draw the image.
					RECT rc = {0, 0, image.Width, image.Height};
					pImage->Draw(dc, &rc, NULL);
					debugprintf("CComHelper 4");
					pImage->Release();
					debugprintf("CComHelper 5");
				}
			}
		}
	}
}